package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Item;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Service
public class StatementItemServiceImpl implements IStatementItemService {
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementMapper statementMapper;
    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery query, Long statementId) {
        //使用paigeHelper插件
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        //将list集合传入到TablePageInfo进行封装
        return new TablePageInfo<StatementItem>(statementItemMapper.selectByStatementId(statementId));
    }

    @Override
    public void saveItem(List<StatementItem> list) {
        //获取list 后拿到最后一个元素, 最后一个元素约定itemPrice为优惠金额  statementId为 服务清单的id
        //使用remove方法获取到最后一个元素
        StatementItem statementItem = list.remove(list.size() - 1);
        //获取优惠价格
        BigDecimal discountAmount = statementItem.getItemPrice();
        //获取statementid
        Long statementId = statementItem.getStatementId();
        //删除关系表中的关系
        //遍历list插入关系表中的数据
        statementMapper.deleteRelation(statementId);
        BigDecimal totalAmount = new BigDecimal("0");
        BigDecimal totalQuantity = new BigDecimal("0");
        for (StatementItem item : list) {
            totalAmount = totalAmount.add(item.getItemPrice().multiply(new BigDecimal(item.getItemQuantity())));
            totalQuantity = totalQuantity.add(new BigDecimal(item.getItemQuantity()));
            statementItemMapper.insert(item);
        }
        //修改statement中的 总金额 优惠金额 总数量
        statementMapper.updateAmountByStatementId(statementId,totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public void pay(Long statementId) {
        //根据id查询出来数据
        //判断数据是否在一个消费中的状态
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if (!(Statement.STATUS_CONSUME.equals(statement.getStatus()))){
            throw new BusinessException("非法操作");
        }
        //获取登录的id
        Long loginUserId = ShiroUtils.getUser().getId();
        statementMapper.changePayStatus(statementId,loginUserId,Statement.STATUS_PAID);
    }

    @Override
    public void saveItem1(Item item) {
        BigDecimal discountAmount = item.getDiscountAmount();

        List<StatementItem> list = item.getList();
        Long statementId = list.get(0).getStatementId();
        //计算总金额 和总数量
        BigDecimal totalAmount = new BigDecimal("0");
        BigDecimal totalQuantity = new BigDecimal("0");
        //删除关系
        //将关系插入到中间表中
        statementMapper.deleteRelation(statementId);
        for (StatementItem statementItem : list) {
            totalAmount = totalAmount.add(statementItem.getItemPrice().multiply(new BigDecimal(statementItem.getItemQuantity())));
            totalQuantity = totalQuantity.add(new BigDecimal(statementItem.getItemQuantity()));
            statementItemMapper.insert(statementItem);
        }
        //修改statement中的 总金额 优惠金额 总数量
        statementMapper.updateAmountByStatementId(statementId,totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public void pay1(Item item) {
        this.saveItem1(item);
        this.pay(item.getStatementId());
    }

    @Override
    public Workbook export(Long statementId) {
        //查询出来需要导出的数据
        Statement statement = statementMapper.selectByPrimaryKey(statementId);

        //创建workbook对象
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建sheet
        XSSFSheet sheet = workbook.createSheet();
        //创建行
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("客户名称");
        row.createCell(1).setCellValue("到店时间");
        row.createCell(2).setCellValue("车牌号码");
        row.createCell(3).setCellValue("车系");
        row.createCell(4).setCellValue("服务类型");
        row.createCell(5).setCellValue("支付时间");
        row.createCell(6).setCellValue("总金额");
        row.createCell(7).setCellValue("消费服务数量");
        row.createCell(8).setCellValue("备注信息");
        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue(statement.getCustomerName());
        //格式化输出时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String actualArrivalTime = simpleDateFormat.format(statement.getActualArrivalTime());

        row1.createCell(1).setCellValue(actualArrivalTime);
        row1.createCell(2).setCellValue(statement.getLicensePlate());
        row1.createCell(3).setCellValue(statement.getCarSeries());
        if (statement.getServiceType()==0){
            row1.createCell(4).setCellValue("维修");
        }else {
            row1.createCell(4).setCellValue("保养");
        }
        String payTime = simpleDateFormat.format(statement.getPayTime());
        row1.createCell(5).setCellValue(payTime);
        row1.createCell(6).setCellValue(statement.getTotalAmount().toString());
        row1.createCell(7).setCellValue(statement.getTotalQuantity().toString());
        row1.createCell(8).setCellValue(statement.getInfo());

        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("服务名称");
        row2.createCell(1).setCellValue("服务价格");
        row2.createCell(2).setCellValue("服务数量");

        List<StatementItem> statementItems = statementItemMapper.selectByStatementId(statementId);
        StatementItem statementItem = null;
        for (int i = 0; i <statementItems.size(); i++) {
            XSSFRow row3 = sheet.createRow(i + 3);
            statementItem = statementItems.get(i);
            row3.createCell(0).setCellValue(statementItem.getItemName());
            row3.createCell(1).setCellValue(statementItem.getItemPrice().toString());
            row3.createCell(2).setCellValue(statementItem.getItemQuantity());

        }
        return workbook;
    }


}
